#!/usr/bin/env python 3.6
# -*- coding: utf-8 -*-
# @Time      :2021/6/21
# @Author    :zhangxuchuo
# @Email     :zhangxuchuo@bbdservice.com
# @File      :TAX55_EntTaxPaymentYear_SC
# @Software  :PyCharm

'''eof
name:申请企业缴税年限
code:TAX55_EntTaxPaymentYear_SC
tableName:
columnName:
groups:场景业务校验-税务
dependencies:TX_SC_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''

import json
import re
import sys
import time,datetime
import pandas as pd
import math
from dateutil import rrule
from dateutil.relativedelta import relativedelta
import io

# reload(sys)
# sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def formatData(table_Name):
    """
    获取数据
    :param table_Name:字典keys
    :return:[{}]
    """
    try:
        data = TX_SC_DSJ["data"].get(table_Name)
        return data if isinstance(data, list) and len(data) > 0 else [{}]

    except:
        return [{}]

def Formate(time):
    import re
    import datetime
    r = re.compile(r'\D+')
    try:
        date = datetime.datetime.strptime(time, '%Y.%m.%d').strftime('%Y%m%d')
    except:
        try:
            date = datetime.datetime.strptime(time, '%Y-%m-%d').strftime('%Y%m%d')
        except:
            try:
                date = datetime.datetime.strptime(time, '%Y/%m/%d').strftime('%Y%m%d')
            except:
                try:
                    tmp = r.sub('', time)
                    date = datetime.datetime.strptime(tmp, '%Y%m%d').strftime('%Y%m%d')
                except:
                    date = None
    return date

def TAX55_EntTaxPaymentYear():
    try:
        syptSwdjxx = formatData('syptSwdjxx')
        now = datetime.datetime.now()
        kyslrq = syptSwdjxx[0].get('kyslrq', '')
        if Formate(kyslrq) not in null_type_list and Formate(kyslrq):
            days = (now - datetime.datetime.strptime(kyslrq, '%Y-%m-%d')).days
            return round(days / 365, 2)
        else:
            return '缺失值'
    except:
        return '缺失值'





if __name__ == '__main__':
    import json
    with open(r'E:\zxc\samples\TX_SC_DSJ.json') as ff:
        TX_SC_DSJ = json.loads(ff.read())['parsedContent']
    cQexecuteTime = ""
    result = TAX55_EntTaxPaymentYear()
    print(result)

result = TAX55_EntTaxPaymentYear()
# 返回（纳税人登记信息(syptSwdjxx)）中开业设立日期（kyslrq）距今年份数。（保留至小数点后两位）